package com.lz.football.module.club.item
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.constants.TipsConst;
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.IItemMouseEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.ImageContain;
	import lz.controls.managers.TipsManager;

	/**
	 * 有选中效果框，
	 * @author Administrator
	 * 
	 */	
	public class BaseItem extends Sprite implements IComponent, IItemMouseEvent
	{
		/**
		 * data内容
		 */
		private var _data:Object;
		/**
		 *皮肤 
		 */
		public var _skin:MovieClip;
		
		/**
		 *效果框 
		 */
		protected var frame:MovieClip;
		/**
		 *回调函数 
		 */
		private var _callBack:Function;
		/**
		 * 选中状态 
		 */
		protected var _select:Boolean;
		/**
		 *容器保存图片容器 
		 */
		public var contain:MovieClip;
		
		public var imageContain:ImageContain;
		/**
		 *tips内容  
		 */
		private var _tips:Object;
		/**
		 *描述 
		 */
		public var des:TextField;
		
		public function BaseItem()
		{
			super();
		}
		
		/**
		 * 初始化界面 
		 * @param view
		 * 
		 */
		public function initUI(view:Object=null):void
		{
			if(view is MovieClip)
			{
				_skin = view as MovieClip;
			}
			else if(view is String)
			{
				_skin =Reflection.createInstance(view as String);
				this.addChild(_skin);
			}
			if(_skin == null)return;	
			contain = _skin.contain;
			frame = _skin.frame;
			if(frame != null)
			{
				frame.visible = false;
				frame.mouseChildren = false;
				frame.mouseEnabled = false;
			}
			des = _skin.des;
			if(des != null)
			{
				des.text = "";
				des.mouseEnabled = false;
			}
			
			addEvtListener();
		}
		/**
		 * 设置图片显示资源 
		 * 
		 * @param value  BitmapData 或者已经在swf中的资源（string）
		 * 
		 */
		public function setImageSorce(value:Object,$width:int = 50,$height:int = 50):void
		{
			if(imageContain == null)
				imageContain = new ImageContain();
			if($width > 0)
				imageContain.width = $width;
			if($height > 0)
				imageContain.height = $height;
			imageContain.source = value;
			contain.addChild(imageContain);
		}
		/**
		 * 设置图片显示资源url 
		 * @param $url 图片地址
		 * 
		 */
		public function setImageUrl($url:String):void
		{
			if(imageContain == null)
				imageContain = new ImageContain();
			imageContain.url = $url;
			contain.addChild(imageContain);
		}
		/**
		 * 增加事件 
		 * 
		 */
		public function addEvtListener():void
		{
			_skin.addEventListener(MouseEvent.CLICK,onMouseClick);
			_skin.addEventListener(MouseEvent.ROLL_OUT,onMouseRollOut);
			_skin.addEventListener(MouseEvent.ROLL_OVER,onMouseRollOver);
		}
		/**
		 * 移除事件 
		 * 
		 */		
		public function removeEvtListener():void
		{
			if(_skin == null)return;
			_skin.removeEventListener(MouseEvent.CLICK,onMouseClick);
			_skin.removeEventListener(MouseEvent.ROLL_OUT,onMouseRollOut);
			_skin.removeEventListener(MouseEvent.ROLL_OVER,onMouseRollOver);
		}
		
		/**
		 *销毁 
		 * 
		 */
		public function destroyView():void
		{
			removeEvtListener();
			if(contain != null)DisplayObjectUtil.removeAllChildren(contain);
			if(this.parent)
				this.parent.removeChild(this);
			if(imageContain != null)
			{
				imageContain.clear();
				imageContain.destroy();
			}
			imageContain = null;
			_callBack = null;
			_data = null;
			_tips = null;
			frame = null;
			contain = null;
			_skin = null;
			
		}
		
		/**
		 * 移出 
		 * @param event
		 * 
		 */
		public function onMouseRollOut(event:MouseEvent):void
		{
			TipsManager.hide();
			if(_select)return;
			if(frame != null)
				frame.visible = false;
			
		}
		
		/**
		 * 移上 
		 * @param event
		 * 
		 */
		public function onMouseRollOver(event:MouseEvent):void
		{
			if(tips is String)
			{
				TipsManager.show(TipsConst.BASETIPS,{tip:tips as String},15,15,false);
			}
			if(_select)return;
			if(frame != null)
				frame.visible = true;
			
		}
		/**
		 * 点击 
		 * @param event
		 * 
		 */		
		public function onMouseClick(event:MouseEvent):void
		{
//			if(_select)return;
			if(_callBack != null)_callBack.apply(null,[this]);
			TipsManager.hide();
		}
		
		/**
		 * 获取data内容 
		 * @return 
		 * 
		 */
		public function get data():Object
		{
			return _data;
		}
		
		public function set data(value:Object):void
		{
			_data = value;
			
			
		}

		/**
		 * 回调函数
		 */
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}

		public function get callBack():Function
		{
			return _callBack;
		}
		/**
		 *选择状态 
		 */
		public function get select():Boolean
		{
			return _select;
		}

		/**
		 * @private
		 */
		public function set select(value:Boolean):void
		{
			_select = value;
			if(frame == null)return;
			if(_select)
				frame.visible = true;
			else
				frame.visible = false;
		}


		/**
		 *重置 
		 * 
		 */
		public function reset():void
		{
			if(imageContain != null)imageContain.clear();
			select = false;
			_data = null;
			tips = null;
		}
		public function get tips():Object
		{
			return 	_tips;
		}
		/**
		 * 
		 * @param value
		 * 
		 */
		public function set tips(value:Object):void
		{
			_tips = value;
		}
	}
}